package main

import (
	"log"

	"github.com/gin-gonic/gin"
	"github.com/kesilent/snippetsbox/models"
)

type Env struct {
	db models.Datastore
}

var env *Env

func main() {
	db, err := models.NewDB("root:root@tcp(127.0.0.1:3306)/snippets_box?charset=utf8")
	if err != nil {
		log.Panic(err)
	}
	env = &Env{db}

	gin.SetMode(gin.DebugMode) //全局设置环境，此为开发环境，线上环境为gin.ReleaseMode
	router := gin.Default()    //获得路由实例
	//添加中间件
	router.Use(Cors())

	v1 := router.Group("api/v1")
	{
		v1.GET("/", Index)
		v1.GET("/tags", GetAllTag)
		v1.GET("/codeall", GetCodeListAll)
		v1.POST("/addtags", AddTag)
		v1.POST("/codefortag", GetCodeListForTagID)
		v1.POST("/addcode",AddCode)
		v1.POST("/codeforid",GetCodeForId)
	}

	router.Run(":8080")
}
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Writer.Header().Add("Access-Control-Allow-Origin", "*")
		c.Next()
	}
}
